package cn.heygod.nacostool.session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author Simon
 */
@Component
public class UserLoginInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public static final String LOGIN_PAGE = "/login.html";

    @Autowired
    private SessionManager sessionManager;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("request url:{}",request.getRequestURL());
        try {
            if(sessionManager.isLogin(request)){
                return true;
            }
            //重定向登录页面
            response.sendRedirect(request.getContextPath() + LOGIN_PAGE);
        } catch (Exception e) {
            logger.error("login filter error:",e);
        }
        return false;
        //如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
        //如果设置为true时，请求将会继续执行后面的操作
    }


}
